package com.zlq.cn.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlq.cn.admin.domain.Admin;
import com.zlq.cn.admin.domain.Menu;
import com.zlq.cn.admin.domain.vo.AdminVo;
import com.zlq.cn.admin.service.AdminService;
import com.zlq.cn.admin.service.AdminTokenService;
import com.zlq.cn.admin.service.MenuService;
import com.zlq.cn.api.SignApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "用户")
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private MenuService menuService;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("登录")
    @PostMapping("/login")
    public SignApiResult<AdminVo> login(String name, String password, HttpServletRequest request) {
        AdminVo admin = adminService.login(name, password);
        AdminTokenService.login(admin);
        return SignApiResult.resultSuccess(admin);
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public SignApiResult<Boolean> logout() {
        AdminTokenService.logout();
        return SignApiResult.resultSuccess(true);
    }

    @ApiOperation("菜单列表")
    @GetMapping("/menus")
    public SignApiResult<List<Menu>> menus() {
        return SignApiResult.resultSuccess(menuService.getAdminMenuList());
    }

    @ApiOperation("人员列表")
    @PostMapping("/list")
    public SignApiResult<Page> list(Page page, String name) {
        adminService.getList(page, name);
        return SignApiResult.resultSuccess(page);
    }

    @ApiOperation("人员删除")
    @PostMapping("/delete")
    public SignApiResult<Boolean> delete(Integer id) {
        adminService.delete(id);
        return SignApiResult.resultSuccess(true);
    }

    @ApiOperation("人员新增")
    @PostMapping("/save")
    public SignApiResult<Boolean> save(Admin admin) {
        adminService.save(admin);
        return SignApiResult.resultSuccess(true);
    }

    @ApiOperation("人员更新密码")
    @PostMapping("/updatePassword")
    public SignApiResult<Boolean> updatePassword(Integer id, String password) {
        adminService.updatePassword(id, password);
        return SignApiResult.resultSuccess(true);
    }

    @ApiOperation("人员启用禁用")
    @PostMapping("/updateEnable")
    public SignApiResult<Boolean> updateEnable(Integer id, Boolean isEnable) {
        adminService.updateEnable(id, isEnable);
        return SignApiResult.resultSuccess(true);
    }

}
